$(document).ready(function () {

    if ($("#btnSave").attr('value') == lang_edit) {
        $("#addInsuranceAttend_name").attr('disabled', 'disabled');
        $("#addInsuranceAttend_description").attr('disabled', 'disabled');
        $("#addInsuranceAttend_regulation").attr('disabled', 'disabled');
        $("#addInsuranceAttend_family").attr('disabled', 'disabled');
        $("#addInsuranceAttend_note").attr('disabled', 'disabled');
        $("#addInsuranceAttend_creationDate").attr('disabled', 'disabled');
    }

    $('#btnSave').click(function () {

        $('#addInsuranceAttend_insuranceAttendId').val(insuranceAttendId);

        // if user clicks on Edit make all fields editable
        if ($("#btnSave").attr('value') == lang_edit) {
            $("#addInsuranceAttend_name").removeAttr("disabled");
            $("#addInsuranceAttend_description").removeAttr("disabled");
            $("#addInsuranceAttend_regulation").removeAttr("disabled");
            $("#addInsuranceAttend_family").removeAttr("disabled");
            $("#addInsuranceAttend_note").removeAttr("disabled");

            $("#btnSave").attr('value', lang_save);
            $("#btnBack").attr('value', lang_cancel);
            return;
        }

        if ($("#btnSave").attr('value') == lang_save) {
            if (isValidForm()) {
                $('form#frmAddInsuranceAttend').attr({
                    action: linkForAddInsuranceAttend + "?Id=" + insuranceAttendId
                });
                $('#frmAddInsuranceAttend').submit();
            }
        }

    });

    $('#btnBack').click(function () {
        if ($("#btnBack").attr('value') == lang_back) {
            window.location.replace(backBtnUrl + '?insuranceAttendId=' + insuranceAttendId);
        }
        if ($("#btnBack").attr('value') == lang_cancel) {
            window.location.replace(backCancelUrl + '?Id=' + insuranceAttendId);
        }
    });
});

function isValidForm() {
    $.validator.addMethod("uniqueName", function (value, element, params) {
        var temp = true;
        var currentInsuranceAttend;
        var id = parseInt(insuranceAttendId, 10);
        var insuranceAttendCount = insuranceAttendNameList.length;
        for (var j = 0; j < insuranceAttendCount; j++) {
            if (id == insuranceAttendNameList[j].id) {
                currentInsuranceAttend = j;
            }
        }
        var i;
        insuranceAttendName = $.trim($('#addInsuranceAttend_name').val()).toLowerCase();
        for (i = 0; i < insuranceAttendCount; i++) {

            arrayName = insuranceAttendNameList[i].name.toLowerCase();
            if (insuranceAttendName == arrayName) {
                temp = false
                break;
            }
        }
        if (currentInsuranceAttend != null) {
            if (vcName == insuranceAttendNameList[currentInsuranceAttend].name.toLowerCase()) {
                temp = true;
            }
        }

        return temp;
    });

    var validator = $("#frmAddInsuranceAttend").validate({

        rules: {
            'addInsuranceAttend[name]': {
                uniqueName: true,
                required: true
            },
            'addInsuranceAttend[description]': {
                maxlength: 500,
                required: true
            },
            'addInsuranceAttend[regulation]': {
                maxlength: 100,
                required: true
            },
            'addInsuranceAttend[family]': {
                maxlength: 400
            },
            'addInsuranceAttend[note]': {
                maxlength: 200
            }
        },
        messages: {
            'addInsuranceAttend[name]': {
                uniqueName: lang_nameExistmsg,
                required: lang_nameRequired
            },
            'addInsuranceAttend[description]': {
                maxlength: lang_descriptionLength,
                required: lang_descriptionRequired
            },
            'addInsuranceAttend[regulation]': {
                maxlength: lang_regulationLength,
                required: lang_regulationRequired
            },
            'addInsuranceAttend[family]': {
                maxlength: lang_familyLength
            },
            'addInsuranceAttend[note]': {
                maxlength: lang_noteLength
            }
        }
    });
    return true;
}